//
//  FDDicJSONTool.m
//  FD
//
//  Created by xiaofang wang on 2019/1/5.
//  Copyright © 2019年 ZhiYuan Network. All rights reserved.
//

#import "FDDicJSONTool.h"

@implementation FDDicJSONTool

/**dic转json*/
+ (NSString *)JSONFromDic:(NSDictionary *)dic {
    if (dic == nil) {
        FDLog(@"Dic为空操作无效");
        return @"";
    }
    
    NSError *error = nil;
    
    //jsondata
    NSData *jsondata = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
    
    if (error) {
        FDLog(@"dic 转 json 失败:%@",error);
        return @"";
    }
    
    //utf8编码
    NSString *jsonStr = [[NSString alloc] initWithData:jsondata encoding:NSUTF8StringEncoding];
    
    return jsonStr;
}


/**JSON ---> DIC*/
+ (NSDictionary *)dicFromJSON:(NSString *)json {
    
    if (json == nil) {
        FDLog(@"为空该操作无效");
        return nil;
    }
    
    //utf8编码
    NSData *data = [json dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error = nil;
    
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    
    if (error) {
        FDLog(@"JSON转换失败:%@",error);
        return nil;
    }
    
    return dic;
}

@end
